<?php
class article
{
    protected $id;
    protected $id_author;
    protected $id_section;
    protected $date;
    protected $counter;    
    protected $header;
    protected $anotation;
    protected $text;
    protected $pending;
    protected $state;    
        
    
    public function __construct($id, $id_author, $id_section, $date, $counter, $header, $anotation, $text, $pending, $note = NULL, $state = NULL)
    {
        $this->id = $id;
        $this->id_section = $id_section;
        $this->id_author = $id_author;
        $this->date = $date;
        $this->counter = $counter;
        $this->header = $header;
        $this->anotation = $anotation;
        $this->text = $text;
        $this->pending = $pending;
        $this->note = $note;
        $this->state = $state;
    }
    public function __toString()
    {
        //podle toho jestli je nastavenej text
        $result = '';
        if(isset($this->text))
        {
            $result .= '<h1>'.$this->header;
        }else
        {
            $result .= sprintf("<h1><a href=\"clanky.php?id=\"%s\">%s</a>",
                    $this->id,
                    $this->header);
        }        
        $result .= '<br /><small>'.$this->anotation.'</small></h1>';
        if(isset($this->text))
        {
            $result .= '<p>'.$this->text.'</p>';
            $result .= '<a href="forum.php?id_article='.$this->id.'">Vstupte do diksuze</a>';//odkaz do diskuze
            //zvysit counter v databazi
            $conn = new dbConnection();
            $query = sprintf("UPDATE articles SET counter = counter +1 WHERE id = '%s'",
                    $this->id);
            $sql_result = $conn->db_query($query);
            if($sql_result == false)
                {
                    die('Invalid query: ' . mysql_error());
                }
        }
        return $result;
    }
    public function updateInfo($id_section,  $header, $anotation, $article, $note, $state, $pending)
    {
        $conn = new dbConnection();        
        $query = sprintf("UPDATE articles SET 
                id_section='%s', 
                header='%s', 
                anotation='%s', 
                article='%s', 
                note='%s', 
                state='%s', 
                pending='%s' 
                WHERE id = '%s'" ,
                $id_section,
                $header,
                $anotation,
                $article,
                $note,
                $state,
                $pending,
                $this->id);
        $sql_result = $conn->db_query($query);
        if (!$sql_result) 
    {
        die('Invalid query: ' . mysql_error().'<br />'. $query);
    }
        
    }
}

?>
